﻿using Common;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using T68Control.Procdure;

namespace T68Control.View
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : Window
    {
        private DateTime firstDate = new DateTime(2024, 6, 25);

        public MainView()
        {
            InitializeComponent();
        }

        public MainView(string msg, string sn)
        {
            InitializeComponent();
            this.cbHome.IsChecked = true;
            tbDevSN.Text = $"设备序列号: {sn}";
            var licenseInfo = msg.Split(',').ToList();
            tbLicenseInfo.Text = $"软件有效期至: {licenseInfo[0]}, 已使用: {(DateTime.Now - firstDate).Days}天";
            tbUserName.Text = Global.User.UserName;
            Task.Run(() =>
            {
                while (true)
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        tbSysTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    });
                    Thread.Sleep(1000);
                }
            });
            if (Global.User.UserLevel == UserLevel.Admin)
            {
                cbS12L_M1.Visibility = Visibility.Visible;
                cbDebug.Visibility = Visibility.Visible;
                cbUserManager.Visibility = Visibility.Visible;
            }
            else
            {
                cbS12L_M1.Visibility = Visibility.Collapsed;
                cbDebug.Visibility = Visibility.Collapsed;
                cbUserManager.Visibility = Visibility.Collapsed;
            }
            cbHome.IsChecked = true;
        }

        private void Border_MouseLeftButtonDoubleDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                if (WindowState == WindowState.Normal)
                {
                    WindowState = WindowState.Maximized;
                }
                else
                {
                    WindowState = WindowState.Normal;
                }
            }
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }

        private void btnSwitchUser_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var loginView = new LoginView();
                loginView.Owner = this;
                var loginResult = loginView.ShowDialog();
                if (loginResult == true)
                {
                    loginView.Close();
                    tbUserName.Text = Global.User.UserName;
                    if (Global.User.UserLevel == UserLevel.Admin)
                    {
                        cbS12L_M1.Visibility = Visibility.Visible;
                        cbDebug.Visibility = Visibility.Visible;
                        cbUserManager.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        cbS12L_M1.Visibility = Visibility.Collapsed;
                        cbDebug.Visibility = Visibility.Collapsed;
                        cbUserManager.Visibility = Visibility.Collapsed;
                    }
                    cbHome.IsChecked = true;
                }
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
            }
        }

        private void btExit_Click(object sender, RoutedEventArgs e)
        {
            if (MyMessageBox.Show("提示", "是否退出窗口?", this) == true)
            {
                //Global.CANfd.ResetAll();
                //Global.CANfd.CloseLight();
                //Global.CANfd.CloseDevice();
                if (Machine.IOModule != null)
                {
                    Machine.IOModule.SetDOValue((int)DODefine.Red, 0);
                    Machine.IOModule.SetDOValue((int)DODefine.Yellow, 0);
                    Machine.IOModule.SetDOValue((int)DODefine.Green, 0);
                }
                this.Close();
                Environment.Exit(0);
            }
        }

        private void btnToggleWin_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
            }
            else
            {
                WindowState = WindowState.Normal;
            }
        }

        private void cbHome_Checked(object sender, RoutedEventArgs e)
        {
            Global.UCHome ??= new HomeView();
            this.mainContent.Content = Global.UCHome;
        }

        private void cbDebug_Checked(object sender, RoutedEventArgs e)
        {
            Global.UCDebug ??= new DebugView();
            this.mainContent.Content = Global.UCDebug;
        }

        private void cbUserManager_Checked(object sender, RoutedEventArgs e)
        {
            Global.UCUserManage ??= new UserManageView();
            this.mainContent.Content = Global.UCUserManage;
        }

        private void cbS12L_M1_Checked(object sender, RoutedEventArgs e)
        {
            Global.UCS12LM1 ??= new S12L_M1View();
            this.mainContent.Content = Global.UCS12LM1;
        }
    }
}